<!-- 后结算模块-财务管理-采购单确认收货 -->
<template>
  <div class="hospital-settlement-change-box">
    <el-container class="container-box is-vertical">
      <record-info
        :record-form="purcharseCommonForm"
        :record-data="headerInfo">
        <template slot="recordInfoslot">
          <SearchForm
            label-width="120px"
            :data="purchaseSearchData"
            :compact-data="purchaseSearchData.slice(0,2)"
            show-collapse
            @searchForm="searchForm"
            @resetForm="resetForm"
            :submit-loading='tableLoading'
          />
          <div
            class="solt-info">
            <span class="left-span">
              采购产品总数
              <span class="right-span">{{ headerInfo.prodTotalCount }}</span>
            </span>
            <span>
              品种数
              <span>{{ headerInfo.prodTypeCount }}</span>
            </span>
            <span>
              金额合计 （元）:
              <span>
                <span class="price">{{ headerInfo.orderAmount | getFormatterPrice }}</span>
              </span>
            </span>
          </div>
        </template>
      </record-info>
      <el-editable
        :data="orderList"
        height="200"
        v-loading="tableLoading"
        :columns="purcharseReceiveTableData"
      >
        <!-- 查看发票详情 -->
        <template
          slot="default-invoiceAction"
          slot-scope="scope">
          <el-button
            @click="toLookDetails(scope.row)"
            type="text"
          >查看详情</el-button>
        </template>
        <!-- 发票影像(记得使用UI给的小图标) -->
        <template
          slot="default-invoiceUrl"
          slot-scope="scope">
          <div style="display:flex; justify-content: center;">
            <el-image
              v-if="scope.row.invoiceUrl"
              @click="toLookDetails(scope.row)"
              style="width: 23px; height: 16.6px;"
              :src="imageicon"
            />
          </div>
        </template>
        <template
          slot="default-registrationNumber"
          slot-scope="scope">
          <el-button
            :disabled="!scope.row.registrationNumber||!scope.row.registrationId"
            v-html="regisNoButton(scope.row)"
            @click="showPdfRegistion(scope.row.registrationId)"
            type="text"
          />
        </template>
      </el-editable>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="curPage"
        :page-sizes="PAGE_NUMBER"
        :page-size="pageSize"
        :layout="PAGINATION_LAYOUT"
        :total="totalElements"
      />
      <invoice-details
        :display-or-not="displayOrNotDetails"
        :invoice-url="activeInvoiceUrl"
        :busness-type="1"
        :invoice-no="activeInvoiceNo"
        @cancelDialog="cancelDialog" />
    </el-container>
  </div>
</template>

<script>
import invoiceDetails from '@/moduleComponents/beforeSettle/invoiceDetails/index.vue'
import {
  hospitalSettlePurchaseWait,
  hospitalSettlePurchaseConfrim
} from '@const/beforeSettleApi'
import { getCertificate } from '@const/productCenterApiV2'
import {
  purcharseReceiveTableData,
  purcharseCommonForm,
  statusArr,
  purchaseSearchData
} from './const'
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
import RecordInfo from '@/moduleComponents/beforeSettle/distributorSide/purchasingManagement/record'
import ElEditable from '@components/el-editable'
import SearchForm from '@components/businesscomponents/search'
import { cloneDeep, isObject, hasIn } from 'lodash'
import { getFormatterPrice } from '@const/filter'
export default {
  components: { ElEditable, RecordInfo, SearchForm, invoiceDetails },
  data () {
    return {
      imageicon: require('@assets/icon/beforeSettle/imageicon.png'),
      id: this.$route.query.id,
      purchaseSearchData,
      // 表格Loading
      tableLoading: false,
      orderList: [],
      // 表头
      purcharseReceiveTableData,
      purcharseCommonForm,
      // 表单 详情展示内容-table
      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0, // 总共的条数
      // 订单详情信息
      headerInfo: {},
      activeInvoiceNo: '',
      activeInvoiceUrl: '',
      displayOrNotDetails: false
    }
  },

  mounted () {
    this._globalBtns()
    this.getList()
  },
  filters: {
    getFormatterPrice
  },
  methods: {
    _globalBtns () {
      let arr = [
        { type: 'primary', val: '确定收货', handle: this.receiveGoods }
      ]
      const btn = {
        type: 'btn',
        arr
      }
      this.$store.commit('changeLayoutHandle', btn)
    },
    // 查看发票详情
    toLookDetails (val) {
      this.activeInvoiceUrl = val.invoiceUrl
      this.activeInvoiceNo = val.invoiceNo
      this.displayOrNotDetails = true
    },
    cancelDialog () {
      this.activeInvoiceUrl = ''
      this.activeInvoiceNo = ''
      this.displayOrNotDetails = false
      this.getList()
    },
    // 搜索功能-点击查询
    searchForm (condition) {
      this.searchData = condition
      this.getList()
    },

    // 点击重置
    resetForm (condition) {
      this.curPage = 1
      this.searchData = condition
    },
    // 获取待收货详情列表
    async getList () {
      this.tableLoading = true
      const res = await this.getOrderList()
      // const res = {}
      let obj = hasIn(res, 'data.payload') ? res.data.payload : {}
      // 处理返回数据
      // 列表字段 'waitReceiveProductList'--采购订单待收货详情
      let { waitReceiveProductList, ...hearerInfo } = obj
      waitReceiveProductList = waitReceiveProductList || { totalElements: 0 }
      // Object.keys(hearerInfo).forEach(key => {
      //   // if (key === 'orderStatus') {
      //   //   obj[key] = statusArr[obj[key]]
      //   // }
      //   this.$set(this.headerInfo, key, hearerInfo[key] || '--')
      // })
      // hearerInfo.orderStatus + ''
      // 解决状态不显示问题
      this.headerInfo = hearerInfo
      this.headerInfo.orderStatus = statusArr[this.headerInfo.orderStatus]

      this.orderList = waitReceiveProductList.content
      this.totalElements = waitReceiveProductList.totalElements || 0
      this.tableLoading = false
    },
    // 待收货详情请求
    async getOrderList () {
      const url = hospitalSettlePurchaseWait
      const data = {
        ...this.searchData,
        page: this.curPage,
        pageSize: this.pageSize,
        purchaseOrderId: this.$route.query.id
      }
      return this.$axios.post(url, data)
    },
    showPdfRegistion (id) {
      this.$axios({
        method: 'get',
        url: getCertificate,
        params: {
          id: id
        }
      }).then(res => {
        let obj = hasIn(res, 'data.payload') ? res.data.payload : {}
        let url = obj.pictureUrl
        if (!url) {
          this.$message({
            type: 'warning',
            message: '获取失败，请在产品管理中维护该产品的注册证信息'
          })
        } else {
          this.$pdfViewer({ url, title: '注册证pdf预览' })
        }
      })
    },
    regisNoButton (data) {
      if (data.registrationNumber) {
        return data.registrationNumber
      } else if (!data.registrationNumber && data.registrationId) {
        return '暂无注册证号'
      } else if (!data.registrationNumber && !data.registrationId) {
        return '暂无数据'
      }
    },
    // 点击收货
    async receiveGoods () {
      const query = {
        purchaseOrderId: this.id
      }
      const { data } = await this.$axios.post(hospitalSettlePurchaseConfrim, query)
      if (data.code === '0') {
        this.$message.success('收货成功')
        this.$router.go(-1)
      }
    },
    // 页码更改
    handleSizeChange (size) {
      this.pageSize = size
      this.getList()
    },
    // 切换不一样的页面,重置一切数据
    handleCurrentChange (curPage) {
      this.curPage = curPage
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
.hospital-settlement-change-box {
  .container-box {
    height: 100%;
    overflow: auto;
    .pagination {
      padding-top: 10px;
      display: flex;
      justify-content: flex-end;
      padding-right: 0;
    }
  }
}
</style>
